//
//  BaseCollectionViewCell.m
//  Wangi
//
//  Created by ChuXiang on 15/11/23.
//  Copyright © 2015年 ChuXiang. All rights reserved.
//

#import "BaseCollectionViewCell.h"
#import "UIView+UIViewAdditions.h"
#import "ContentTableViewCell.h"
#import "ScrollWithImage.h"
#import "BaseModel.h"
#import "SencondModel.h"
#import "ThreeImageTableViewCell.h"
#import "MJRefresh.h"
#import "OneImageTableViewCell.h"
@interface BaseCollectionViewCell () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) ScrollWithImage *scroll;
@end
@implementation BaseCollectionViewCell

- (void)dealloc {
    [_table release];
    [_scroll release];
    [_data release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self addSubview:self.table];
    }
    return self;
}

- (void)setData:(NSMutableArray *)data {
    if (_data != data) {
        [_data release];
        _data = [data retain];
    }
    if (_data.count >  0) {
        NSArray *array = [_data[0] adsArray];
    self.scroll = [[ScrollWithImage alloc] initWithFrame:CGRectMake(0, 0, self.width, 200) withData:array];
        _table.tableHeaderView = _scroll;
    }
    [_table reloadData];
}

- (void) applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    self.table.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 20);
}

#pragma mark - TableViewDataSource, TableViewDelegate
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _data.count - 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[_data[indexPath.row + 1] skipType] isEqualToString:@"photoset"]) {
        return 120;
    }
    NSString *string = [[_data[indexPath.row + 1] imgType] stringValue];
    if ([string isEqualToString:@"1"]) {
        return 150;
    }
    return 75;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_data.count > 0) {
        if ([[_data[indexPath.row + 1] skipType] isEqualToString:@"photoset"]) {
            ThreeImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThreeImageTableViewCellIdentifier"];
            cell.model = _data[indexPath.row + 1];
            return cell;
        }
        NSString *string = [[_data[indexPath.row + 1] imgType] stringValue];
        if ([string isEqualToString:@"1"]) {
            OneImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneImageTableViewCellIdentifier"];
            cell.model = _data[indexPath.row + 1];
            return cell;
        }
        ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContentTableViewCellIdentifier"];
        cell.model = _data[indexPath.row + 1];
        return cell;
    }
    return nil;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.delegate setModel:_data[indexPath.row]];
}

- (UITableView *)table {
    if (!_table) {
        self.table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_table registerClass:[ContentTableViewCell class] forCellReuseIdentifier:@"ContentTableViewCellIdentifier"];
        [_table registerClass:[ThreeImageTableViewCell class] forCellReuseIdentifier:@"ThreeImageTableViewCellIdentifier"];
        [_table registerClass:[OneImageTableViewCell class] forCellReuseIdentifier:@"OneImageTableViewCellIdentifier"];
        [self addHeader];
        //        程序启动时header自动启动刷新
        [_table headerBeginRefreshing];
        _table.delegate = self;
        _table.dataSource = self;
    }
    return _table;
}

- (void) addHeader {
    [_table addHeaderWithCallback:^{
        //当进去刷新状态后回调这个block
        //模拟延迟加载数据,因此2秒后才调用
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // tableView刷新列表
            [_table reloadData];
            //刷新完成后结束header的刷新状态
            [_table headerEndRefreshing];
        });
    }];
}

@end
